{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编程语言都有数据类型：\n",
    "整形(int),浮点型(float)，str,bool(0/1,True, False)\n",
    "1bit 0/1 两种状态\n",
    "1byte=8bit 2**8=256种状态，ascii码用一个字节去表示,a, A\n",
    "int 4 byte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 <class 'int'> 10 <class 'str'> 10 <class 'int'>\n",
      "10 10 10\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = str(a)\n",
    "c = int(b)\n",
    "print(a, type(a), b, type(b), c, type(c))\n",
    "print(a, b, c)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True False\n"
     ]
    }
   ],
   "source": [
    "d = True\n",
    "e = False\n",
    "print(d, e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10\n",
    "print(a, type(a))\n",
    "\n",
    "b = 10.3\n",
    "print(b, type(b))\n",
    "\n",
    "c = True\n",
    "d = False\n",
    "print(c, d, type(c), type(d))\n",
    "\n",
    "e = 'I am from China'\n",
    "print(e, type(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sequence 序列\n",
    "tuple, list\n",
    "tuple(元组，定值列表)\n",
    "list列表\n",
    "[下限索引:上限索引:步长]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2.0, 'test') <class 'tuple'>\n",
      "[0.1, 0.2, 'abc'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "tu1 = (1, 2.0, 'test')\n",
    "tu2 = ([0.1, 0.2, 'abc'])\n",
    "print(tu1, type(tu1))\n",
    "print(tu2, type(tu2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2.0\n",
      "0.1 0.2\n"
     ]
    }
   ],
   "source": [
    "print(tu1[0], tu1[1])\n",
    "print(tu2[0], tu2[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32md:\\workspace\\opensource\\experimental_manual\\software_engineering_practices\\programming_languages\\python_from_scratch\\data_type_basic_operations.ipynb Cell 8\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/d%3A/workspace/opensource/experimental_manual/software_engineering_practices/programming_languages/python_from_scratch/data_type_basic_operations.ipynb#X21sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m tu1[\u001b[39m0\u001b[39m] \u001b[39m=\u001b[39m \u001b[39m2\u001b[39m\n\u001b[0;32m      <a href='vscode-notebook-cell:/d%3A/workspace/opensource/experimental_manual/software_engineering_practices/programming_languages/python_from_scratch/data_type_basic_operations.ipynb#X21sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m \u001b[39mprint\u001b[39m(tu1)\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "tu1[0] = 2\n",
    "print(tu1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.3, 0.2, 'abc']\n"
     ]
    }
   ],
   "source": [
    "tu2[0] = 0.3\n",
    "print(tu2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.3, 0.2, 'abc', 0.5]\n"
     ]
    }
   ],
   "source": [
    "tu2 += [0.5]\n",
    "print(tu2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.0, 2.0] <class 'list'>\n",
      "[7, 5.0]\n"
     ]
    }
   ],
   "source": [
    "tu3 = ('a', 1, 2, 3, 4, 5, [1.0, 2.0])\n",
    "print(tu3[6], type(tu3[6]))\n",
    "tu3[6][0] = 7\n",
    "c = tu3[6]\n",
    "c[1] = 5.0\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "('a', 1, 2, 3, 4)\n",
      "('a', 1, 2, 3, 4)\n"
     ]
    }
   ],
   "source": [
    "print(tu3[3])\n",
    "print(tu3[0:5:1])\n",
    "print(tu3[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5, [7, 5.0])\n",
      "('a', 1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "print(tu3[1:])\n",
    "print(tu3[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([7, 5.0], 5, 4, 3, 2, 1)\n"
     ]
    }
   ],
   "source": [
    "print(tu3[-1:0:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([7, 5.0], 5, 4, 3, 2, 1, 'a')\n"
     ]
    }
   ],
   "source": [
    "print(tu3[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([7, 5.0], 5, 4, 3, 2, 1, 'a')\n"
     ]
    }
   ],
   "source": [
    "print(tu3[-1::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#元组不能二次赋值，相当于只读列表\n",
    "s1 = (2, 1.3, 'love', 5.6, 9 ,12, True, False, ('nest', 'tuple', 1, 2.0), ['list', 1])\n",
    "s2 = ['I', 5, 4.0, True, [1, 2]]\n",
    "print(s1, type(s1))\n",
    "print(s2, type(s2))\n",
    "\n",
    "print(s1[3], s2[3])\n",
    "print(s1[0:5:1], s1[0:5])\n",
    "print(s1[0:5:2])\n",
    "print(s1[1:-1], s1[1:], s1[:5])\n",
    "print(s2[-1:0:-1])\n",
    "print(s2[-1::-1])\n",
    "\n",
    "test = 'the middle autumn day is coming'\n",
    "print(test[:10])\n",
    "\n",
    "#s1[0] = 2.0\n",
    "s2[0] = 3.0\n",
    "print(s2)\n",
    "\n",
    "#以下对元组的操作是无效的，因为元组不允许更新，而列表是允许更新的：\n",
    "tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )\n",
    "list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]\n",
    "tuple[2] = 1000    # 元组中是非法应用\n",
    "list[2] = 1000     # 列表中是合法应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数学运算：+，-，*，/, %, //, **\n",
    "判断：==，!=, <, <=, >, >=, in\n",
    "逻辑运算： and, or, not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1+9\n",
    "b = 1-9\n",
    "c = 1/9\n",
    "d = 1//9\n",
    "e = 3%5\n",
    "f = 7*9\n",
    "s = 2**3\n",
    "print(a, b, c, d, e, s)\n",
    "\n",
    "g = 3 >= 1\n",
    "h = 2 < 5\n",
    "i = 8 != 8\n",
    "print(g, h, i)\n",
    "\n",
    "print(1 > 0 and 9 < 10)\n",
    "print(1 < 0 or 2 > 1)\n",
    "print(not True)\n",
    "print(not False)\n",
    "print(2 in [2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(3 in [3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(3 in [4, 5, 6])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if True:\n",
    "    sentence 1\n",
    "    sentence 2\n",
    "elif cond:\n",
    "    sentence 3\n",
    "elif **:\n",
    "    ****\n",
    "else:\n",
    "    ****\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 10\n",
    "if i > 0:\n",
    "    print(i)\n",
    "    print(i*2)\n",
    "\n",
    "height = 170\n",
    "gender = '女'\n",
    "\n",
    "if height > 180:\n",
    "    print('high')\n",
    "elif height < 160:\n",
    "    if gender == '男':\n",
    "        print('short')\n",
    "elif height <= 170:\n",
    "    if gender == '女':\n",
    "        print('high')\n",
    "else:\n",
    "    print('unknown')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "gxy = np.array([1.6, 0.7], np.float)\n",
    "print(gxy %1.)#取小数点后面的数\n",
    "print(gxy >1.)#判断是否大于1\n",
    "j =  ((gxy %1. < 0.5) & (gxy > 1.))\n",
    "print(j)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组，numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float32)\n",
    "\n",
    "print(arr)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
